/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display.miniboard;

import deatax.ResourceLoader;
import deatax.display.Texture;
import deatax.display.TileRenderer;
import java.util.ArrayList;
import java.util.List;
import javax.media.opengl.GL2;

/**
 *
 * @author pson
 */
public class MiniBoardTile {

    public static float TILE_HEIGHT = 0.3f;
    public static float TILE_BEVEL = 0.1f;
    private String type;
    public int x;
    public int y;
    public float height = 1;
    public float[] color;
    public float[] line;
    List<MiniMarker> markers = new ArrayList<MiniMarker>();
    public static float[][] markerPositions = genPositions();
    private static List<String> tile_types = new ArrayList<String>();
    public static float[] hex = TileRenderer.genHex(5);
    public static float[] hex_inner = TileRenderer.genHex(4.5f);
    public static float[] hex_texture = TileRenderer.genHexP(0.5f);
    public Texture texture;

    public MiniBoard miniboard;
    
    public static float[][] genPositions() {
        float[][] pos = new float[10][];
        pos[0] = new float[]{0, 0};
        pos[1] = pos[0];
        for (int i = 2; i < pos.length; i++) {
            pos[i] = TileRenderer.genPolygon(i, 0.5f);
        }
        return pos;
    }

    public MiniBoardTile(int x, int y, String type, MiniBoard miniboard) {
        this.x = x;
        this.y = y;
        this.type = type;
        if (type.equals("Death")) {
            color = new float[]{0.6f, 0.5f, 0.6f};
            line = new float[]{0.9f, 0.8f, 0.9f};
        } else if (type.equals("Tax")) {
            color = new float[]{0.4f, 0.4f, 0.7f};
            line = new float[]{0.7f, 0.7f, 0.9f};
        } else {
            this.type = null;
            color = new float[]{0.7f, 0.7f, 0.7f};
            line = new float[]{0.9f, 0.9f, 0.9f};
        }
        try {
            type = type.toLowerCase();
            if (ResourceLoader.resourceExists("display/miniboard/" + type + ".png")) {
            //if (Main.class.getResource("display/miniboard/" + type + ".png") != null) {
                texture = new Texture("display/miniboard/" + type + ".png");
            } else {
                System.out.println("No - miniboard/" + type + ".png");
            }
        } catch (Exception ex) {
        }
        this.miniboard = miniboard;
    }

    public List<MiniMarker> getMarkers() {
        return markers;
    }

    public void addMarker(MiniMarker m) {
        markers.add(m);
//        System.out.println("Added marker: " + m.toString() + " ("+markers.size() +")");
        updateMarkers();
    }

    public void removeMarker(MiniMarker m) {
        markers.remove(m);
        updateMarkers();
    }

    public void updateMarkers() {
        int i = 0;
        for (MiniMarker marker : markers) {
            float[] p = miniboard.mapToGrid(x + 1, y);//-markerPositions[markers.size()][i]-markerPositions[markers.size()][i+1]
            marker.setPosition(p[0] - markerPositions[markers.size()][i], p[1] - markerPositions[markers.size()][i + 1]);
            i += 2;
        }
    }

    public String getType() {
        return type;
    }

    public void draw(GL2 gl) {
        gl.glColor3f(0.95f, 0.95f, 0.95f);

        gl.glBegin(GL2.GL_TRIANGLE_STRIP);
        for (int i = 0; i < 13; i += 2) {
            gl.glVertex3f(hex_inner[i % 12], hex_inner[i % 12 + 1], height);
            gl.glVertex3f(hex[i % 12], hex[i % 12 + 1], height - TILE_BEVEL);
        }
        gl.glEnd();
        gl.glColor3f(0.7f, 0.7f, 0.7f);
        gl.glBegin(GL2.GL_TRIANGLE_STRIP);
        for (int i = 12; i > -1; i -= 2) {
            gl.glVertex3f(hex[i % 12], hex[i % 12 + 1], 0);
            gl.glVertex3f(hex[i % 12], hex[i % 12 + 1], height);
        }
        gl.glEnd();
        if (texture != null) {
            gl.glColor3f(1, 1, 1);
            gl.glEnable(GL2.GL_TEXTURE_2D);
            texture.bind(gl);
            gl.glColor3f(0.9f, 0.9f, 0.9f);
            gl.glBegin(GL2.GL_TRIANGLE_FAN);
            for (int i = 0; i < 12; i += 2) {
                gl.glTexCoord2fv(hex_texture, i);
                gl.glVertex3f(hex_inner[i], hex_inner[i + 1], TILE_BEVEL + height);
            }
            gl.glEnd();
            gl.glDisable(GL2.GL_TEXTURE_2D);
        } else {
            gl.glColor3f(0.9f, 0.9f, 0.9f);
            gl.glBegin(GL2.GL_TRIANGLE_FAN);
            for (int i = 0; i < 12; i += 2) {
                gl.glVertex3f(hex_inner[i], hex_inner[i + 1], TILE_BEVEL + height);
            }
            gl.glEnd();
        }
    }
    
    public float[] getCoords(){
        float[] f = miniboard.mapToGrid(x + 1, y);
        f[2] = height;
        return f;
    }
    
}
